//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;

using CommunityServer.Components;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls 
{

	/// <summary>
	/// This server control displays a list of all forums with grouping and formatting to make them readable.
	/// </summary>
	public class ForumDropDownList : DropDownList 
	{
        CSContext csContext;

		/// <remarks>
		/// Fills ForumDropDownList with data.
		/// </remarks>
		public ForumDropDownList() 
		{
            csContext = CSContext.Current;
            
   	        DataBind();
		}

		public override void DataBind() 
		{
			ArrayList forumGroups;

			// Get all forum groups
			if (this.ForumMode == ControlUserMode.User)
				forumGroups = ForumGroups.GetForumGroups(false, false);
			else
				forumGroups = ForumGroups.GetForumGroups(false);
            
			foreach (Group group in forumGroups) 
			{
				User user = CSContext.Current.User;

				// quick hack to keep recursive from showing
				if (Items.FindByValue("g-" + group.GroupID) == null) 
				{

					// Get the sections for the group
					if(this.ForumMode == ControlUserMode.User)
						group.Sections = Forums.GetForumsByForumGroupID(group.GroupID,true,false);
					else
						group.Sections = Forums.GetForumsByForumGroupID(group.GroupID,false,true);

					if(group.HasSections) 
					{
						// Add the forum group
						//
						Items.Add(new ListItem(group.Name, "g-" + group.GroupID));

						RecursiveAddForum (0,  group.Sections);

						// Add the forum group
						//
						Items.Add(new ListItem("", "s"));

					}

				}

			}

            if (Items.FindByValue("f--1") == null)
			    Items.Insert(0, new ListItem( "All Forums", "f--1"));

            // Select current forum if its ID is provided via GET
            if (csContext.SectionID > 0) {
                this.SelectedForum = csContext.SectionID;
            }
		}

		public int SelectedForum 
		{
			get 
			{
				if (base.SelectedValue.StartsWith("f-"))
					return int.Parse(base.SelectedValue.Replace("f-", ""));
				return 0;
			}
			set 
			{
				if (value > 0) 
				{
                    // If something selected, then unselect
                    //
                    if (base.SelectedValue != "")
                    {
                        Items.FindByValue( base.SelectedValue ).Selected = false;
                    }
                    
                    // Select provided value
                    //
                    ListItem selectedItem = Items.FindByValue("f-" + value);
                    if (selectedItem != null)
                        selectedItem.Selected = true;
				}
			}
		}


		public int SelectedForumGroup 
		{
			get 
			{
				if (base.SelectedValue.StartsWith("f")) 
				{
					return Forums.GetForum(int.Parse(base.SelectedValue.Replace("f-", ""))).GroupID;
				} 
				else if(base.SelectedValue.StartsWith("g"))
				{
					return int.Parse(base.SelectedValue.Replace("g-", ""));
				}
				else
					return 0;
			}
			set 
			{ 
				// Deselect current item
				if (base.SelectedValue != "")
					Items.FindByValue( base.SelectedValue ).Selected = false;

				Items.FindByValue("g-" + value).Selected = true;
			}
		}

		private void RecursiveAddForum (int depth, ArrayList forums) 
		{

            
			foreach (Forum forum in forums) 
			{
				// We only go 3 deep
				//
				switch (depth) 
				{
					case 0:
						Items.Add(new ListItem(ResourceManager.GetString("Navigation_JumpDropDownList_Indent1") + forum.Name, "f-" + forum.SectionID.ToString()));
						if (forum.Sections.Count > 0)
						{
							// Sort subforums by name
							forum.Sections.Sort(new SectionSorter(SectionSortBy.Name));

							RecursiveAddForum((depth + 1), forum.Sections);
						}
						break;

					case 1:
						Items.Add(new ListItem(ResourceManager.GetString("Navigation_JumpDropDownList_Indent2") + forum.Name, "f-" + forum.SectionID.ToString()));
						if (forum.Sections.Count > 0)
						{
							// Sort subforums by name
							forum.Sections.Sort(new SectionSorter(SectionSortBy.Name));

							RecursiveAddForum((depth + 1), forum.Sections);
						}
						break;

					case 2:
						Items.Add(new ListItem(ResourceManager.GetString("Navigation_JumpDropDownList_Indent3") + forum.Name, "f-" + forum.SectionID.ToString()));
						if (forum.Sections.Count > 0)
							RecursiveAddForum((depth + 1), forum.Sections);
						break;

					default:
						return;

				}
			}
		}


		[
		System.ComponentModel.DefaultValue( ControlUserMode.User ),
		]
		public virtual ControlUserMode ForumMode 
		{
			get 
			{
				Object state = ViewState["ForumMode"];
				if ( state != null ) 
				{
					return (ControlUserMode)state;
				}
				return ControlUserMode.User;
			}
			set 
			{
				ViewState["ForumMode"] = value;
			}
		}
	}
}


